package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.*;
import com.monitor.entity.*;
import com.monitor.service.ErpBidataService;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import com.monitor.util.SortingTpms;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆BI表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ErpBidataServiceImpl extends ServiceImpl<ErpBidataMapper, ErpBidata> implements ErpBidataService {

	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpBidataMapper erpBidataMapper;
	
	@Resource
	private ErpBidataListMapper erpBidataListMapper;

	@Resource
    private  ErpUserMapper erpUserMapper;
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpSystemParametersChildMapper erpSystemParametersChildMapper;
	
	@Override//String cs,HttpServletRequest request
	public Result<List<List<String>>> selBiData(Integer userId,String cs) {

		// TODO Auto-generated method stub
        ErpUser user = erpUserMapper.selectById(userId);
		//创建返回值
		List<List<String>> datas = new ArrayList<List<String>>();
		List<String> firseData = new ArrayList<String>();
		firseData.add("序号");
		firseData.add("客户名称");
		firseData.add("车辆牌号");
		firseData.add("自编号");
		firseData.add("车辆类型");
		firseData.add("车轴/轮数");
		//获取当前登陆客户
//		ErpCustomer thiscus = (ErpCustomer) request.getSession().getAttribute("");
		//获取当前登陆人
//		ErpUser user = (ErpUser) request.getSession().getAttribute("");
		//获取当前单位,下级单位,管辖单位
//		String code="%"+thiscus.getCusCode()+"%";
		//暂时先测试固定传个id
		List<ErpCustomer> cuss0 = erpCustomerMapper.selectByIdOrCompanysAndServiceComes(1,"%8%","%8%");//thiscus.getId(), code, code
		//获取有业务的单位 有业务的客户都会存在于使用频率表
		List<ErpCustomer> cuss1 = erpCustomerMapper.selectByCusCode(1);//thiscus.getId()
		
		//创建客户id列
		List<Integer> cusidList = new ArrayList<Integer>();
		List<ErpCustomer> cuss = new ArrayList<ErpCustomer>();
		//创建客户id字段
		String cusids = "-1";
		for(ErpCustomer cus:cuss0){
			if(cusidList.indexOf(cus.getId()) == -1){
				cusids += ","+cus.getId();
				cusidList.add(cus.getId());
				cuss.add(cus);
			}
		}
		for(ErpCustomer cus:cuss1){
			if(cusidList.indexOf(cus.getId()) == -1){
				cusids += ","+cus.getId();
				cusidList.add(cus.getId());
				cuss.add(cus);
			}
		}
		
		//获取当前登录客户的车辆
		List<ErpCustomerCar> cars = erpCustomerCarMapper.selectByCusids(cusids);

		//获取当前客户车辆的bi信息
		List<ErpBidata> bis = erpBidataMapper.selectByCusids(cusids);
		//获取系统参数中的BI接口
		List<ErpSystemParametersChild> spcJk = erpSystemParametersChildMapper.selectByPid(66);
		//创建一个列保存接口参数的ID 用判断顺序
		List<Integer> jkids = new ArrayList<Integer>();
		for(ErpSystemParametersChild spc:spcJk){
			firseData.add(spc.getName());
			jkids.add(spc.getId());
		}
		//获取系统参数中的车载BI
		List<ErpSystemParametersChild> spcCZ =  erpSystemParametersChildMapper.selectByPid(54);
		List<Integer> czids = new ArrayList<Integer>();
		for(ErpSystemParametersChild spc:spcCZ){
			firseData.add(spc.getName());
			czids.add(spc.getId());
		}
		firseData.add("录入人");
		//datas.add(firseData);
		//创建保存车辆牌号
		List<String> plates = new ArrayList<String>();
		for(ErpBidata bi:bis){
			plates.add(bi.getCarPlate());
		}
		for(ErpCustomerCar car:cars){
			int index = plates.indexOf(car.getCarPlate());
			List<String> data = new ArrayList<String>();
			ErpCustomer cus = cuss.get(cusidList.indexOf(car.getCustomerId()));
			if(index == -1){
				ErpBidata bi = new ErpBidata();
				bi.setCusId(cus.getId());
				bi.setCarPlate(car.getCarPlate());
				bi.setCarNum(car.getCarNumber());
				bi.setCarType(car.getCarType1());
				bi.setTirePressure(car.getCarWheel());
				bi.setUserId(user.getId());
				bi.setUserName(user.getRealName());
				bi.setAddDate(DataUtil.getStringDateShort());
				if(erpBidataMapper.insert(bi)>0){
					data.add(String.valueOf(bi.getId()));
					data.add(String.valueOf(cus.getCusCode()));
					data.add(String.valueOf(car.getId()));
					data.add(String.valueOf(datas.size()));
					data.add(cus.getCusName());
					data.add(car.getCarPlate());
					data.add(car.getCarNumber());
					data.add(car.getCarType1());
					data.add(car.getCarWheel());
					for(ErpSystemParametersChild spc:spcJk){
						data.add("");
						ErpBidataList bl = new ErpBidataList();
						bl.setBiId(bi.getId());
						bl.setParameterId(spc.getId());
						bl.setParameterName(spc.getName());
						bl.setParameterValue("");
						bl.setParameterType(0);
						erpBidataListMapper.insert(bl);
					}
					for(ErpSystemParametersChild spc:spcCZ){
						data.add("");
						ErpBidataList bl = new ErpBidataList();
						bl.setBiId(bi.getId());
						bl.setParameterId(spc.getId());
						bl.setParameterName(spc.getName());
						bl.setParameterValue("");
						bl.setParameterType(1);
						erpBidataListMapper.insert(bl);
					}
					data.add((bi.getAddDate())+"  "+user.getRealName());
					datas.add(data);
				}
			}else{
				ErpBidata bi = bis.get(index);
				data.add(String.valueOf(bi.getId()));
				data.add(String.valueOf(cus.getCusCode()));
				data.add(String.valueOf(car.getId()));
				data.add(String.valueOf(datas.size()));
				data.add(cus.getCusName());
				data.add(bi.getCarPlate());
				data.add(bi.getCarNum());
				data.add(bi.getCarType());
				data.add(bi.getTirePressure());
				for(int i=0; i<jkids.size()+czids.size(); i++){
					data.add("");
				}
				//查询参数
				List<ErpBidataList> bls = erpBidataListMapper.selectByBiId(bi.getId());
				for(ErpBidataList bl:bls){
					if(bl.getParameterType()==0){
						int ind = jkids.indexOf(bl.getParameterId());
						if(ind>-1){
							data.set(ind+9, bl.getParameterValue());
						}
					}else{
						int ind = czids.indexOf(bl.getParameterId());
						if(ind>-1){
							data.set(ind+9+jkids.size(), bl.getParameterValue());
						}
					}
				}
				data.add(bi.getAddDate()+"  "+bi.getUserName());
				datas.add(data);
			}
		}
		if(!BlankUtil.isBlank(cs)){
			List<List<String>> datas0 = new ArrayList<List<String>>();
			datas0.add(datas.get(0));
			for(int i=1; i<datas.size(); i++){
				List<String> data = datas.get(i);
				if(data.get(1).indexOf(cs)>-1 || data.get(4).indexOf(cs)>-1 || data.get(5).indexOf(cs)>-1){
					datas0.add(data);
				}
			}
			for(int i=1; i<datas0.size(); i++){
				List<String> data = datas0.get(i);
				data.set(2, String.valueOf(i));
				datas0.set(i, data);
			}
			SortingTpms sor = new SortingTpms();
			Collections.sort(datas0, sor);
			return new Result<List<List<String>>>(datas0);
		}else{
			SortingTpms sor = new SortingTpms();
			Collections.sort(datas, sor);
			return new Result<List<List<String>>>(datas);
		}
	}

	@Override
	public Result<List<ErpBidataList>> toUpdateBiDate(int id) {
		// TODO Auto-generated method stub
		List<ErpBidataList> list=erpBidataListMapper.selectList(new EntityWrapper<ErpBidataList>().eq("bi_id", id).and().eq("parameter_type", 0));
		return new Result<List<ErpBidataList>>(list,list.size());
	}

	@Override
	public Result<Boolean> updateBidata(String carPlate,String ids, String values) {
		// TODO Auto-generated method stub
		ErpBidata bidata=erpBidataMapper.selectByPlate(carPlate);
		erpBidataListMapper.delete(new EntityWrapper<ErpBidataList>().eq("bi_id", bidata.getId()));
		Boolean pan=true;
		String[] id = ids.split(",");
		String[] value = values.split(",");
		for(int i=0; i<id.length; i++){
			try {
				ErpBidataList bi=new ErpBidataList();
				bi.setBiId(bidata.getId());
				if(id[i].equals("胎压接口")) {
					bi.setParameterId(624);
					bi.setParameterType(0);
				}else if(id[i].equals("GPS接口")) {
					bi.setParameterId(625);
					bi.setParameterType(0);
				}else if(id[i].equals("GPS卡号")) {
					bi.setParameterId(666);
					bi.setParameterType(1);
				}else if(id[i].equals("胎压卡号")) {
					bi.setParameterId(665);
					bi.setParameterType(1);
				}
				bi.setParameterName(id[i]);
				
				bi.setParameterValue(value[i]);
				erpBidataListMapper.insert(bi);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				pan= false;
			}
		}
		return new Result<Boolean>(pan, 1);
	}

	@Override
	public Result<List<Map<String, String>>> selectByCarId(String carPlate) {
		// TODO Auto-generated method stub
		try {
			List<Map<String, String>> list=new ArrayList<>();
			List<ErpSystemParametersChild> child=erpSystemParametersChildMapper.selectByPid(54);
			//ErpCustomerCar car=erpCustomerCarMapper.selectByPlate(carPlate);
			ErpBidata bi=erpBidataMapper.selectByPlate(carPlate);
			child.forEach(item->{
				Map<String, String> map=new HashMap<>();
				ErpBidataList bidata=erpBidataListMapper.selectByBiIdAndName(bi.getId(), item.getName());
				if(bidata!=null) {
					map.put(item.getName(), bidata.getParameterValue());
				}else {
					map.put(item.getName(), "");
				}
				list.add(map);
			});
			return new Result<List<Map<String, String>>>(list, list.size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

}
